<?php

namespace App\Models;

class IncreaseBase extends Model
{
    protected $table = 'yf_increase_base';
    protected $primaryKey = 'increase_id';
    public $timestamps = false;
    protected $guarded = ['increase_id'];

    public function increaseGoods()
    {
        return $this->hasMany(IncreaseGoods::class, 'increase_id', 'increase_id');
    }

    public function increaseRules()
    {
        return $this->hasMany(IncreaseRule::class, 'increase_id', 'increase_id');
    }

    public function increaseRedempGoods()
    {
        return $this->hasMany(IncreaseRedempGoods::class, 'increase_id', 'increase_id');
    }

    /**
     * 商品加价购详情
     * @param int $goodsId
     * @return array
     */
    public static function getIncreaseDetailByGoodsId(int $goodsId) : array
    {
        $res = [];
        $now = now()->toDateTimeString();
        $increaseGoods = IncreaseGoods::where([
                ['goods_id', '=', $goodsId],
                ['goods_start_time', '<=', $now],
                ['goods_end_time', '>=', $now]
            ])
            ->first();
        if ($increaseGoods) {
            $increaseBase = self::with([
                'increaseGoods',
                'increaseRules' => function($query) {
                    return $query->oldest('rule_price');
                },
                'increaseRedempGoods' => function($query) {
                    return $query->oldest('redemp_price');
                }
            ])
            ->find($increaseGoods->increase_id);

            // 参加活动的商品
            $goodsIds = $increaseBase->increaseGoods->pluck('goods_id')->toArray();
            $goodsList = GoodsBase::getGoodsListByGoodsId($goodsIds);
            $res['goods'] = $goodsList->toArray();

            // 获取 yf_goods_base 已经不存在的商品，然后从 yf_increase_goods 中删除
            $diffGoodsIds = array_diff($goodsIds, $goodsList->pluck('goods_id')->toArray());
            IncreaseGoods::whereIn('goods_id', $diffGoodsIds)->delete();

            $increaseBase->increaseRules
                ->whereIn('rule_id', $increaseBase->increaseRedempGoods->pluck('rule_id')->toArray())
                ->each(function ($rule, $index) use (&$res, $increaseBase) {
                    $res['rule'][$index] = $rule->toArray();
                    $increaseBase->increaseRedempGoods()
                        ->with('goodsBase')
                        ->each(function($redempGoods, $i) use (&$res, $rule, $index) {
                            if ($redempGoods->rule_id == $rule->rule_id) {
                                $res['rule'][$index]['redemption_goods'][$i] = $redempGoods->only('redemp_goods_id', 'redemp_price', 'goods_id');
                                $res['rule'][$index]['redemption_goods'][$i]['goods_spec'] = $redempGoods->goodsBase->getGoodsSpecName();
                                $res['rule'][$index]['redemption_goods'][$i] = array_merge($res['rule'][$index]['redemption_goods'][$i], $redempGoods->goodsBase->only('goods_name', 'goods_price', 'goods_image', 'goods_stock'));
                            }
                        });
                });
        }

        return $res;
    }
}
